************************************************************
*****     		Create relevant samples			       *****
************************************************************

/* CONTENTS:
- Set Paths
- Create program
- Set Delta and age upper bound

- Hospitalizations
1. Overall sample
2. Robustness Check - mutual shock

*/


********************************************
* Set folders and paths
********************************************
cd $processed_data
cap mkdir !temp
cd !temp

********************************************
* Program to create precise subsets of treatment vars
********************************************
cap program drop IndexVar
program IndexVar
	macro drop var*
	args varlist
	des `varlist', varlist
	local variables = r(varlist)
	local N : word count `variables'
	local group = 50
	global index= ceil(`N'/`group') //split variables into groups of 50, take ceiling
	local endpoint = `group'
	global varS `: word 1 of `variables'' //index first variable
	forvalues i = 1/$index {
		if `endpoint' >= `N' { //index last variable
			global varL `: word `N' of `variables''	
		}
		else { //create start and end points to precisely define groups
			local startpoint = `endpoint'-1
			global var`i'_s `: word `startpoint' of `variables''
			global var`i'_e `: word `endpoint' of `variables''
		}
		local endpoint = `group'*(`i'+1)
	}
end 

exit 

********************************************
* Set Delta and age upper bound
********************************************
global delta = 4
if $delta == 2 {
	global age_UB=18
}
else if $delta == 3 {
	global age_UB=18
}
else if $delta == 4 {
	global age_UB=18
}
else if $delta == 5 {
	global age_UB=18
}

*********************************************************
****************** Hospitalizations *********************
*********************************************************

*********************************************
*** 1. Overall sample
*********************************************
{
timer clear 1
timer on 1
forvalues childbyear= 1990(1)2008 {
	
	use "$processed_data\sample_healthshock.dta", clear 
	
	keep if child_b_year == `childbyear'
	
	g yearinphospital = year(first_inphospital)
	g ageinphospital = yearinphospital - child_b_year
	keep if ageinphospital >= 6 & ageinphospital <= $age_UB
	keep if mother_b_year >= 1945 & mother_b_year <=1992
	keep if father_b_year >= 1922 & father_b_year <=1992
	
	egen bdyid = group(mother_b_year father_b_year)
	compress 
	
	// Create binary variables representing treatment groups 
	/// for each mother birthyear, father birthyear, and child's age at health shock 
	levelsof bdyid, local(bdy_levels)
	foreach bdy of local bdy_levels {
		qui levelsof ageinphospital, local(age_levels)
		foreach age of local age_levels {
			gen byte treat_`age'_`bdy' = . 
			replace treat_`age'_`bdy' = 1 if ///
			ageinphospital== `age' & bdyid == `bdy' 
			replace treat_`age'_`bdy' = 0 if ///
			ageinphospital==(`age'+ $delta) & bdyid == `bdy'
			qui su treat_`age'_`bdy', de 
			if r(min) == r(max) {
				drop treat_`age'_`bdy'
			}
		}
	}
	save "temp_MB.dta", replace 
	
	// Create a set of global variables that index the above binary variables
	use "temp_MB.dta", clear 

	capture IndexVar treat*
	if _rc == 111 { //variable treat* not found (occurs for years 2008-2015 in delta4);
		save "tf`childbyear'_all_$delta.dta", replace
		continue 
	}
	
	// Reshape horizontally chunked data so it is long, uniquely identified
	/// by mother's ID and treatment group
	if $index <= 2 { //too few variables to run loop (occurs in 2007)
	    reshape long treat_, i(lopenr_mor) j(group) string
		rename treat_ treat
		duplicates report lopenr_mor
		drop if missing(treat)
		rename ageinphospital ageinphospitalnew

		split group, p("_")
		destring group1, replace
		rename group1 ageinptreat

		save "tf`childbyear'_all_$delta.dta", replace 
	}
	
	else {
		local index = $index - 1
		forvalues i = 1/`index' {
			use "temp_MB.dta", clear 	
			if `i' == 1 { //startpoint is special case
				keep lopenr_mor-bdyid ${varS}-${var2_s} 
			}
			else if `i' == `index' { //endpoint is a special case 
				local startindex = `i'
				keep lopenr_mor-bdyid ${var`startindex'_e}-${varL}
			}
			else {
				local startindex = `i'
				local endindex = `startindex' + 1
				keep lopenr_mor-bdyid ${var`startindex'_e}-${var`endindex'_s}
			}
			reshape long treat_, i(lopenr_mor) j(group) string
			rename treat_ treat
			duplicates report lopenr_mor
			drop if missing(treat)
			
			save "tf`childbyear'_`i'.dta", replace
		}

		
		// Append and remove datasets for child birth year 
		local index = $index - 1
		use "tf`childbyear'_1.dta", clear
		forvalues i=2(1)`index' {
			append using "tf`childbyear'_`i'.dta"
			rm "tf`childbyear'_`i'.dta"
		}

		rename ageinphospital ageinphospitalnew

		split group, p("_")
		destring group1, replace
		rename group1 ageinptreat
		drop group group2 
		
		save "tf`childbyear'_all_$delta.dta", replace 

		rm "tf`childbyear'_1.dta"
	}
}
timer off 1
timer list 
// 4802 seconds (1 hour and 20 min) for delta == 4
// 4802 seconds (1 hour and 20 min) for delta == 3
// 4757 seconds (1 hour and 20 min) for delta == 2

local index = $index - 1
use "tf1990_all_$delta.dta", clear
forvalues i=1991(1)2008 {
	append using "tf`i'_all_$delta.dta"
}

* Drop missing treat values 
** Occurs when no possible treatment-control pairings exist for a given child birth year.
drop if missing(treat) 

compress
save "$processed_data\tf_all_${delta}.dta", replace 
}

*********************************************
*** 2. Robustness Check
**** Mutual shock
*********************************************
{

foreach time in "month" "week" {
	forvalues childbyear= 1990(1)2008 {
		
		use "$processed_data\matched_mutual_`time'.dta", clear 
		
		keep if child_b_year == `childbyear'
		
		g yearinphospital = year(first_inphospital)
		g ageinphospital = yearinphospital - child_b_year
		
		keep if ageinphospital>=6 & ageinphospital <= $age_UB
		keep if mother_b_year >= 1945 & mother_b_year <=1992
		keep if father_b_year >= 1922 & father_b_year <=1992
		
		egen bdyid = group(mother_b_year father_b_year)
		compress 
		
		// Create binary variables representing treatment groups 
		/// for each mother birthyear, father birthyear, and child's age at health shock 
		levelsof bdyid, local(bdy_levels)
		foreach bdy of local bdy_levels {
			qui levelsof ageinphospital, local(age_levels)
			foreach age of local age_levels {
				gen byte treat_`age'_`bdy' = . 
				replace treat_`age'_`bdy' = 1 if ///
				ageinphospital== `age' & bdyid == `bdy' 
				replace treat_`age'_`bdy' = 0 if ///
				ageinphospital==(`age'+ $delta) & bdyid == `bdy'
				qui su treat_`age'_`bdy', de 
				if r(min) == r(max) {
					drop treat_`age'_`bdy'
				}
			}
		}
		save "temp_MB.dta", replace 
		
		// Create a set of global variables that index the above binary variables
		use "temp_MB.dta", clear 

		capture IndexVar treat*
		if _rc == 111 { //variable treat* not found (occurs for years 2008-2015 in delta4);
			save "tf`childbyear'_all_$delta.dta", replace
			continue 
		}
		
		// Reshape horizontally chunked data so it is long, uniquely identified
		/// by mother's ID and treatment group
		if $index <= 2 { //too few variables to run loop (occurs in 2007)
			reshape long treat_, i(lopenr_mor) j(group) string
			rename treat_ treat
			duplicates report lopenr_mor
			drop if missing(treat)
			rename ageinphospital ageinphospitalnew

			split group, p("_")
			destring group1, replace
			rename group1 ageinptreat

			save "tf`childbyear'_all_$delta.dta", replace 
		}
		
		else {
			local index = $index - 1
			forvalues i = 1/`index' {
				use "temp_MB.dta", clear 	
				if `i' == 1 { //startpoint is special case
					keep lopenr_mor-bdyid ${varS}-${var2_s} 
				}
				else if `i' == `index' { //endpoint is a special case 
					local startindex = `i'
					keep lopenr_mor-bdyid ${var`startindex'_e}-${varL}
				}
				else {
					local startindex = `i'
					local endindex = `startindex' + 1
					keep lopenr_mor-bdyid ${var`startindex'_e}-${var`endindex'_s}
				}
				reshape long treat_, i(lopenr_mor) j(group) string
				rename treat_ treat
				duplicates report lopenr_mor
				drop if missing(treat)
				
				save "tf`childbyear'_`i'.dta", replace
			}

			
			// Append and remove datasets for child birth year 
			local index = $index - 1
			use "tf`childbyear'_1.dta", clear
			forvalues i=2(1)`index' {
				append using "tf`childbyear'_`i'.dta"
				rm "tf`childbyear'_`i'.dta"
			}

			rename ageinphospital ageinphospitalnew

			split group, p("_")
			destring group1, replace
			rename group1 ageinptreat
			drop group group2 
			
			save "tf`childbyear'_all_$delta.dta", replace 

			rm "tf`childbyear'_1.dta"
		}
	}
	timer off 1
	timer list 


	local index = $index - 1
	use "tf1990_all_$delta.dta", clear
	forvalues i=1991(1)2008 {
		append using "tf`i'_all_$delta.dta"
	}

	* Drop missing treat values 
	** Occurs when no possible treatment-control pairings exist for a given child birth year
	drop if missing(treat) 

	compress
	save "$processed_data\tf_all_`time'_${delta}.dta", replace 
}
}

